import {Component} from '@angular/core';
import {HttpClient} from "@angular/common/http";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  titleId = ``
  isLogin = false
  isRegister = false
  errorMsg = ``
  infoMsg = ``
  userLogin: any = {
    username: '',
    password: ''
  }
  loginData: any = {}
  data1 = ``
  data2 = ``

  constructor(
    private http: HttpClient
  ) {
  }

  reset() {
    this.isLogin = false
    this.isRegister = false
    this.userLogin = {}
    this.errorMsg = ``
    this.infoMsg = ``
  }

  toRegister() {
    this.reset()
    this.isRegister = true
  }

  toLogin() {
    this.reset()
  }

  register() {
    let param = {
      TitleId: this.titleId,
      Username: this.userLogin.username,
      Password: this.userLogin.password,
      RequireBothUsernameAndEmail: false
    }
    this.http.post(`https://${this.titleId}.playfabapi.com/Client/RegisterPlayFabUser`, param).subscribe(r => {
      console.log('注册返回', r)
      this.toLogin()
      this.infoMsg = `登陆成功`
    }, e => {
      console.log('注册返回err', e)
      switch (e?.error?.errorCode) {
        case(1006):
          this.errorMsg = '电子邮件地址不可用';
          break;
        case(1290):
          this.errorMsg = 'EncryptionKeyMissing';
          break;
        case(1490):
          this.errorMsg = 'EvaluationModePlayerCountExceeded';
          break;
        case(1005):
          this.errorMsg = '无效电子邮件地址';
          break;
        case(1193):
          this.errorMsg = 'InvalidPartnerResponse';
          break;
        case(1008):
          this.errorMsg = '无效密码';
          break;
        case(1007):
          this.errorMsg = '无效用户名';
          break;
        case(1058):
          this.errorMsg = '姓名不可用';
          break;
        case(1234):
          this.errorMsg = 'ProfaneDisplayName';
          break;
        case(1302):
          this.errorMsg = 'SignedRequestNotAllowed';
          break;
        case(1009):
          this.errorMsg = '用户名不可用';
          break;
        default:
          this.errorMsg = `注册失败，请检查用户名及密码`
      }
    })
  }

  login() {
    let param = {
      TitleId: this.titleId,
      Username: this.userLogin.username,
      Password: this.userLogin.password,
    }
    this.http.post(`https://${this.titleId}.playfabapi.com/Client/LoginWithPlayFab`, param).subscribe((r: any) => {
      console.log('登陆返回', r)
      this.loginData = r.data
      this.loginData.Username = this.userLogin.username
      console.log('登陆数据', this.loginData)
      this.reset()
      this.isLogin = true
      this.infoMsg = `登陆成功`
    }, e => {
      console.log('登陆返回err', e)
      switch (e?.error?.errorCode) {
        case(1273):
          this.errorMsg = `InvalidSignature`;
          break;
        case(1003):
          this.errorMsg = `用户名或密码无效`;
          break;
        case(1303):
          this.errorMsg = `RequestViewConstraintParamsNotAllowed`;
          break;
        default:
          this.errorMsg = `登陆失败，请检查用户名及密码`
      }
    })
  }

  getUserData() {
    let param = {
      PlayFabId: this.loginData.PlayFabId,
      Keys: ['testKey1', 'testKey2']
    }
    let option = {
      headers: {
        'X-Authorization': this.loginData.SessionTicket
      }
    }
    this.http.post(`https://${this.titleId}.playfabapi.com/Client/GetUserData`, param, option).subscribe((r: any) => {
      console.log('获取用户数据返回', r)
      this.infoMsg = `拉取成功`
      this.data1 = r.data.Data.testKey1.Value
      this.data2 = r.data.Data.testKey2.Value
    })

  }

  updateUserData() {
    let param = {
      Data: {
        testKey1: this.data1,
        testKey2: this.data2
      }
    }
    let option = {
      headers: {
        'X-Authorization': this.loginData.SessionTicket
      }
    }
    this.http.post(`https://${this.titleId}.playfabapi.com/Client/UpdateUserData`, param, option).subscribe((r: any) => {
      console.log('上传数据到云端', r)
      this.infoMsg = `上传成功`
    })
  }

  logout() {
    this.reset()
  }
}
